package com.qf.sl.youshare.activity;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.*;
import android.widget.TextView;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.fragments.StorePlatformFragment;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/7.
 * 18:33
 */
public class SubCategoryActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_category);

        //第一步通过 平台产品分类点击,进入到这个界面
        //  进入之后,传递一个点击的那个分类的 ID
        //  更新 StorePlatformFragment 的设计
        //  使其支持动态的分类列表获取

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {

            //设置返回按钮
            actionBar.setDisplayHomeAsUpEnabled(true);

            //去掉 ActionBar 上的图标
            actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

            //标题居中
            actionBar.setDisplayShowCustomEnabled(true);

            //从 layout 文件夹中加载布局,然后通过代码修改标题
            LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.actionbar_main_title, null);

            //获取 view 中的 TextView 进行标题的设置
            TextView textView = (TextView) view.findViewById(R.id.actionbar_title);
            if (textView != null) {
                textView.setText("商城");
            }

            actionBar.setCustomView(
                    view,
                    new ActionBar.LayoutParams(
                            Gravity.CENTER_HORIZONTAL)
            );

            actionBar.setBackgroundDrawable(new ColorDrawable(0xFF1194D3));
//            actionBar.setBackgroundDrawable(new ColorDrawable(R.color.store_bg));

            actionBar.setDisplayShowTitleEnabled(false);
        }

        Intent intent = getIntent();

        String cid = intent.getStringExtra(Constants.INTENT_KEY_CATEGORY_ID);

        if (cid == null) {
            cid = "2";
        }

        FragmentManager fragmentManager = getSupportFragmentManager();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        StorePlatformFragment fragment = new StorePlatformFragment();

        //设置子分类ID 用于显示特定分类下的内容

        Bundle args = new Bundle();

        //设置子分类ID
        args.putString(Constants.ARG_SUB_CATEGORY_ID,cid);

        fragment.setArguments(args);

        transaction.add(R.id.sub_category_container, fragment);

        transaction.commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.store, menu);

        return true;
    }

    /**
     * actionBar中菜单键的监听
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int featureId = item.getItemId();

        switch (featureId){
            case R.id.action_register://点击注册菜单
                Intent registerIntent = new Intent(this, UserRegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.action_login:
                Intent loginIntent = new Intent(this, LoginActivity.class);
                startActivity(loginIntent);
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}